var http = require("http");
var url = require("url");
var common = require('./common/common');
var domain = require('domain');

function start(route,handle) {
    function onRequest(request, response) {
        var pathname = url.parse(request.url).pathname;
        var postData = "";
        var myDomain = domain.create();
        //检查绑定cookie
        common.bindCookie(request,response,myDomain);
        //收集前端传递的数据，路由
        myDomain.run(function(){
            request.addListener("data", function(postDataChunk) {
                postData += postDataChunk;
            });
            request.addListener("end", function() {
                route(pathname,handle,postData,response);
            });
            console.log("Request for " + pathname + " received.");
        });
    }

//    process.on('uncaughtException', function (err) {
//        console.log(err);
//    });
    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}

exports.start = start;